package com.jin.webvideo.base.spring.interceptor;

import com.jin.webvideo.base.util.ObjectsUtil;
import com.jin.webvideo.service.ConfigService;
import com.jin.webvideo.util.sys.*;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PageMappingInterceptor implements HandlerInterceptor, InterceptorChainItem {
    private ConfigService configService;
    // 不进行映射的页面
    private String[] excludePathMapping = ObjectsUtil.Empty.STRINGS;
    private PathMatcher matcher = new AntPathMatcher();

    public void excludePathMapping(String... patterns){
        this.excludePathMapping = patterns;
    }

    public PageMappingInterceptor(ConfigService configService){
        this.configService = configService;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        if (matchMapping(request.getRequestURI()) && null != modelAndView && !modelAndView.isEmpty()){
            String viewName = modelAndView.getViewName();
            // 重设模板名称
            modelAndView.setViewName( viewName(viewName, request) );
        }
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }

    /**
     * 重设模板名称
     * @param viewName 原模板名称
     * @param request
     * @return
     */
    private String viewName(String viewName, HttpServletRequest request){
        // 不处理转发和重定向
        if (-1 != viewName.indexOf(":")){
            return viewName;
        }

        // 代码中指定
        int index = viewName.lastIndexOf("#");
        // 入参指定
        String suffix = request.getParameter("sys");
        if (-1 != index){
            suffix = viewName.substring(index + 1);
            viewName = viewName.substring(0, index);
        }

        // 原始值不做处理
        if ("o".equals(suffix)) {
            return viewName;
        }
        if (null == suffix) {
            suffix = "";
        }
        // 设置前缀
        String prefix = switch (suffix) {
            case "p" -> "pc/";
            case "m" -> "mobile/";
            case "c" -> "common/";
            default -> "";
        };

        // 自动选择
        if (prefix.isEmpty()){
            prefix = "pc/";
            if (SystemUtil.isMobile(request.getHeader("User-Agent"))){
                prefix = "mobile/";
            }
        }
        return prefix + viewName;
    }

    /**
     * path映射匹配判断
     * @param path
     * @return
     */
    private boolean matchMapping(String path){
        return !match(excludePathMapping, path);
    }

    private boolean match(String[] patterns, String path){
        if (patterns == null || path == null){
            return false;
        }
        for (String pattern : patterns){
            if (matcher.match(pattern, path)){
                return true;
            }
        }
        return false;
    }
}
